package com.fengke.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ReflectionUtil {
	
	public static Object parseStringToObject(String objStr) {
		Class<?> demo = null;
		try {
			demo = Class.forName(objStr.substring(0, objStr.indexOf("@")));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Object obj = null;
		try {
			obj = demo.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		Method [] methods = demo.getMethods();
		String [] objAttrs = objStr.substring(objStr.indexOf("[") + 1, objStr.length() - 1).split(",");
		for(int i = 0; i < objAttrs.length; i ++) {
			if(objAttrs[i] != null && !"".equals(objAttrs[i].trim()) && !objAttrs[i].endsWith("<null>")) {
				String[] attr = objAttrs[i].split("=");
				/******
				 * 适用于知道方法参数类型的情况
				try {
					Method method = demo1.getMethod("set" + attr[0].substring(0, 1).toUpperCase() + attr[0].substring(1), String.class);
					if("java.lang.Long".equals(method.getParameterTypes()[0].getName())) {
						method.invoke(chann1, Long.parseLong(attr[1]));
					} else if("java.lang.Integer".equals(method.getParameterTypes()[0].getName()) || "int".equals(method.getParameterTypes()[0].getName())) {
						method.invoke(chann1, Integer.parseInt(attr[1]));
					} else if("java.lang.Short".equals(method.getParameterTypes()[0].getName()) || "short".equals(method.getParameterTypes()[0].getName())) {
						method.invoke(chann1, Short.parseShort(attr[1]));
					} else if("java.lang.Boolean".equals(method.getParameterTypes()[0].getName()) || "boolean".equals(method.getParameterTypes()[0].getName())){
						method.invoke(chann1, Boolean.parseBoolean(attr[1]));
					} else if("java.util.Date".equals(method.getParameterTypes()[0].getName())) {
						method.invoke(chann1, parseDate(attr[1]));
					} else {
						method.invoke(chann1, attr[1]);
					}
				} catch (SecurityException e1) {
					e1.printStackTrace();
				} catch (NoSuchMethodException e1) {
					e1.printStackTrace();
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				******/
				
				for(int j = 0; j < methods.length; j ++) {
					if(methods[j].getName().startsWith("set") && attr[0].equalsIgnoreCase(methods[j].getName().replace("set", ""))) {
						try {
							if(attr.length >= 2) {
								if("java.lang.Long".equals(methods[j].getParameterTypes()[0].getName())) {
									methods[j].invoke(obj, Long.parseLong(attr[1]));
								} else if("java.lang.Integer".equals(methods[j].getParameterTypes()[0].getName()) || "int".equals(methods[j].getParameterTypes()[0].getName())) {
									methods[j].invoke(obj, Integer.parseInt(attr[1]));
								} else if("java.lang.Short".equals(methods[j].getParameterTypes()[0].getName()) || "short".equals(methods[j].getParameterTypes()[0].getName())) {
									methods[j].invoke(obj, Short.parseShort(attr[1]));
								} else if("java.lang.Boolean".equals(methods[j].getParameterTypes()[0].getName()) || "boolean".equals(methods[j].getParameterTypes()[0].getName())){
									methods[j].invoke(obj, Boolean.parseBoolean(attr[1]));
								} else if("java.util.Date".equals(methods[j].getParameterTypes()[0].getName())) {
									methods[j].invoke(obj, parseDate(attr[1]));
								} else {
									methods[j].invoke(obj, attr[1]);
								}
							} else {
								if("java.lang.Long".equals(methods[j].getParameterTypes()[0].getName())) {
									methods[j].invoke(obj, new Long(0));
								} else if("java.lang.Integer".equals(methods[j].getParameterTypes()[0].getName()) || "int".equals(methods[j].getParameterTypes()[0].getName())) {
									methods[j].invoke(obj, new Integer(0));
								} else if("java.lang.Short".equals(methods[j].getParameterTypes()[0].getName()) || "short".equals(methods[j].getParameterTypes()[0].getName())) {
									methods[j].invoke(obj, new Short((short) 0));
								} else if("java.lang.Boolean".equals(methods[j].getParameterTypes()[0].getName()) || "boolean".equals(methods[j].getParameterTypes()[0].getName())){
									methods[j].invoke(obj, new Boolean(false));
								} else if("java.util.Date".equals(methods[j].getParameterTypes()[0].getName())) {
									methods[j].invoke(obj, new Date());
								} else {
									methods[j].invoke(obj, "");
								}
							}
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		return obj;
	}

	private static Date parseDate(String string) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date reDate = null;
		try {
			reDate = sdf.parse(string);
		} catch (ParseException e) {
			sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			try {
				reDate = sdf.parse(string);
			} catch (ParseException e1) {
				sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US);
				try {
					reDate = sdf.parse(string);
				} catch (ParseException e2) {
					sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.CHINA);
					try {
						reDate = sdf.parse(string);
					} catch (ParseException e3) {
						sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.CHINESE);
						try {
							reDate = sdf.parse(string);
						} catch (ParseException e4) {
							sdf = new SimpleDateFormat("yyyyMMdd");
							try {
								reDate = sdf.parse(string);
							} catch (ParseException e5) {
								sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
								try {
									reDate = sdf.parse(string);
								} catch (ParseException e6) {
									sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
									try {
										reDate = sdf.parse(string);
									} catch (ParseException e7) {
										sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
										try {
											reDate = sdf.parse(string);
										} catch (ParseException e8) {
											e8.printStackTrace();
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return reDate;
	}
}
